﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WiTeach.Tools;
using System.Security.Cryptography;

namespace WiTeach.LoginManagement.DAL
{
    public class UserDAL
    {
        private static string tableName = "ta_user";
        //    private static string tableName = "student_table";

        /// <summary>
        /// 通过用户ID 获取条数
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static int getUserCountByName(string userName)
        {
            //string sql = "select count(*) from " + tableName + " where username = \"" + userName + "\"";
            string sql = "select count(*) from " + tableName + " where username = '" + userName + "' or mobile= '" + userName + "'";

            return Convert.ToInt32(SqlHelper.ExecuteScalar(sql));
        }


        /// <summary>
        /// 验证用户
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool checkUser(string userName, string password)
        {

            string sql = "select password from " + tableName + " where username = '" + userName + "' or mobile= '" + userName + "'";
            string userpwd = SqlHelper.ExecuteScalar(sql).ToString();
            if (password == userpwd)
            {
                return true;
            }
            return false;
        }

        public static string mduser(string str, string user_auth_key = "Astonep@tp-admin!@#$")
        //public static string mduser(string str, string user_auth_key = "1")
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] userkey_data = System.Text.Encoding.UTF8.GetBytes(user_auth_key);
            byte[] userkey_md5_data = md5.ComputeHash(userkey_data);
            string str_userkey = null;
            for(int i=0;i< userkey_md5_data.Length;i++){
                str_userkey = str_userkey + userkey_md5_data[i].ToString("x2");
            }
            byte[] user_data = System.Text.Encoding.UTF8.GetBytes(str_userkey+str);
            byte[] user_md5_data = md5.ComputeHash(user_data);
            string str_user = null;
            for (int i = 0; i < user_md5_data.Length; i++)
            {
                str_user = str_user +  user_md5_data[i].ToString("x2");
            }
            return str_user;
        }


    }

}